<?php

namespace app\index\controller;

use classes\RedisClass;
use classes\TokenBucket;
use service\LogService;
use think\Controller;
use think\Db;
use think\facade\Cache;
use think\Queue;
use think\facade\Env;
use service\WechatService;

class Food extends Controller
{


    public function __construct()
    {

        parent::__construct();

        if (!Env::get('app_debug') && !isMobile()) {
            tip_alert('请在微信手机端访问！');
        }

        if (Env::get('app_debug')) {
            session('wxd_openid', 'o6rBgxER1najTK-KGVmQSa9iPZig');
        }

    }

    function auth($url = '')
    {

        if (!empty($url)) {
            session('callback_url', $url);
        }
        if (isset($_GET['code']) && !empty($_GET['code'])) {
            $appid = sysconf('wechat_appid');
            $appsecret = sysconf('wechat_appsecret');
            $code = $_GET['code'];
            $weixinurl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $appid . "&secret=" . $appsecret . "&code=" . $code . "&grant_type=authorization_code ";
            //获取openid
            $res = curl($weixinurl);
            $token = json_decode($res, true);

            if (isset($token['openid'])) {
                $openid = $token['openid'];
                session("wxd_openid", $openid);
                $callback_url = session('callback_url');
                header("Location:$callback_url");
                exit(0);

            }
        } else {
            if (Env::get('app_debug')) {
                $this->redirect('http://sy.zoudingyong.com/codetoany/getcode.php?auk=demo1');
            } else {
                $this->redirect('http://sy.zoudingyong.com/codetoany/getcode.php?auk=foodmap');
            }

        }


    }

    /**
     * 美食地图首页
     * @return mixed
     */
    public function index()
    {

        $wecha_id = session("wxd_openid");
        if (!$wecha_id) {
            $this->auth($this->request->url(true));
        }
        $config = RedisClass::get_vote_config_info();

        $city = Db::name('tp_region')
            ->field('id city_id,name')
            ->where('parent_id', 18)
            ->order('sort desc')
            ->cache(0)
            ->select();
        //分享朋友圈
        $current_url = $this->request->url(true);
        try {
            $wx_config = [
                'appid' => sysconf('wechat_appid'),
                'appsecret' => sysconf('wechat_appsecret'),
            ];
            $wechat = \WeChat\Script::instance($wx_config);
            $signPackage = $wechat->getJsSign($current_url);
        } catch (\Exception $e) {
            $signPackage = [
                'appId' => sysconf('wechat_appid'),
                'timestamp' => '',
                'nonceStr' => '',
                'signature' => '',
            ];
        };

        return $this->fetch('index', [
            'city' => $city,
            'config' => $config,
            'current_url' => $current_url,
            'signPackage' => $signPackage,
        ]);
    }


    /**
     * 美食地图详情页
     * @return mixed
     */
    public function detail()
    {

        $wecha_id = session("wxd_openid");
        if (!$wecha_id) {
            $this->auth($this->request->url(true));
        }

        $city = Db::name('tp_region')
            ->field('id city_id,name')
            ->where('parent_id', 18)
            ->order('sort desc')
            ->cache(0)
            ->select();

        //分享朋友圈
        $current_url = $this->request->url(true);
        try {
            $wx_config = [
                'appid' => sysconf('wechat_appid'),
                'appsecret' => sysconf('wechat_appsecret'),
            ];
            $wechat = \WeChat\Script::instance($wx_config);
            $signPackage = $wechat->getJsSign($current_url);
        } catch (\Exception $e) {
            $signPackage = [
                'appId' => sysconf('wechat_appid'),
                'timestamp' => '',
                'nonceStr' => '',
                'signature' => '',
            ];
        };

        return $this->fetch('detail', [
            'city' => $city,
            'current_url' => $current_url,
            'signPackage' => $signPackage,
        ]);
    }

}
